package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.CareprojectVo;
import com.woniuxy.oldmanapi.cvo.DeliveryVo;
import com.woniuxy.oldmanapi.entity.Careproject;
import com.woniuxy.oldmanapi.entity.Caretype;
import com.woniuxy.oldmanapi.service.CareProjectService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
public class CareProjectController {
    @Autowired
    CareProjectService careProjectService;

    //查询服务项目--根据服务类别和项目名字
    @GetMapping("/careproject/searchList")
    public ResponseResult<PageInfo<CareprojectVo>> list(String pname,
                                                        @RequestParam(value = "caretypeId", defaultValue = "0") int caretypeId,
                                                        @RequestParam(value = "size", defaultValue = "6") int size,
                                                        @RequestParam(value = "now", defaultValue = "1") int now) {

        ResponseResult<PageInfo<CareprojectVo>> result = new ResponseResult<>();
        try {
            PageInfo<CareprojectVo> pageInfo = careProjectService.findCareproject(caretypeId, pname, size, now);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);

        }
        return result;
    }

    //添加护理项目
    @PostMapping("/careproject/add")
    public ResponseResult<Void> add(@RequestBody Careproject careproject){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
            careProjectService.addcareproject(careproject);
            result.setMsg("添加成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return  result;
    }

    //修改护理项目
    @PostMapping("/careproject/modify")
    public ResponseResult<Void> modify(@RequestBody Careproject careproject){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
            careProjectService.modifyCareproject(careproject);
            result.setMsg("修改成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg("修改失败");
            result.setCode(201);
        }
        return  result;
    }

    //删除护理项目
    @GetMapping("/careproject/remove/{id}")
    public ResponseResult<Void> remove(@PathVariable int id){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
            careProjectService.removeCareproject(id);
            result.setMsg("删除成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setCode(201);
        }
        return  result;
    }
}
